﻿Public Class FormOperPSQX1
    Public usr As String
    Dim objGt As New clsGt
    Dim objGtResultDetails As New clsGtResultDetails
    Dim objGtNgReasonDetails As New clsGtNgReasonDetails
    Dim objGtProcedure As New clsGtProcedure
    Dim objGtProcedureDetails As New clsGtProcedureDetails
    Dim dt As DataTable
    Private Sub FormOperPSQX1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        setData()
    End Sub

    Public Sub setData()
        dt = objGt.getGt("1", "'0'")
        dt.Columns.Add("oper_type_name")
        If dt.Rows.Count > 0 Then
            For Each row As DataRow In dt.Rows
                If row("oper_type") = "0" Then
                    row("oper_type_name") = "骨架板"
                Else
                    row("oper_type_name") = "法兰板"
                End If
            Next
        End If
        dgv.AutoGenerateColumns = False
        dgv.DataSource = dt
    End Sub

    Public Sub setDgvOperData()
        Dim dtProcedure As DataTable = objGtProcedure.selectGtProcedure(dgv.SelectedRows(0).Cells("sys_id").Value.ToString(), "1")
        DataGridView3.AutoGenerateColumns = False
        DataGridView3.DataSource = dtProcedure
        Dim dtOper As DataTable = objGtProcedureDetails.getGtProcedureDetailsList(dgv.SelectedRows(0).Cells("sys_id").Value.ToString(), "1")
        dgvOper.AutoGenerateColumns = False
        dgvOper.DataSource = dtOper
    End Sub

    Public Sub SetDgvResultData0()
        Dim dtResult0 As DataTable = objGtResultDetails.getGtResultDetailsList(dgv.SelectedRows(0).Cells("sys_id").Value.ToString(), "00")
        DataGridView1.AutoGenerateColumns = False
        DataGridView1.DataSource = dtResult0
    End Sub
    Public Sub SetDgvResultData1()
        Dim dtResult1 As DataTable = objGtResultDetails.getGtResultDetailsList(dgv.SelectedRows(0).Cells("sys_id").Value.ToString(), "1")
        DataGridView2.AutoGenerateColumns = False
        DataGridView2.DataSource = dtResult1
    End Sub

    Public Sub setDgvNgData()
        Dim dtNg As DataTable = objGtNgReasonDetails.getGtNgDetailsList(dgv.SelectedRows(0).Cells("sys_id").Value.ToString(), "00")
        dgvNg.AutoGenerateColumns = False
        dgvNg.DataSource = dtNg
    End Sub

    Public Sub setDgvNgOperData()
        Dim dtNgOper As DataTable = objGtNgReasonDetails.getGtNgDetailsList(dgv.SelectedRows(0).Cells("sys_id").Value.ToString(), "1")
        dgvNgOper.AutoGenerateColumns = False
        dgvNgOper.DataSource = dtNgOper
    End Sub

    Private Sub dgv_SelectionChanged(sender As Object, e As EventArgs) Handles dgv.SelectionChanged
        If dgv.SelectedRows.Count > 0 Then
            setDgvOperData()
            SetDgvResultData0()
            setDgvNgData()
            SetDgvResultData1()
            setDgvNgOperData()
        Else
            DataGridView3.AutoGenerateColumns = False
            DataGridView3.DataSource = New DataTable()
            dgvOper.AutoGenerateColumns = False
            dgvOper.DataSource = New DataTable()
            DataGridView1.AutoGenerateColumns = False
            DataGridView1.DataSource = New DataTable()
            DataGridView2.AutoGenerateColumns = False
            DataGridView2.DataSource = New DataTable()
            dgvNg.AutoGenerateColumns = False
            dgvNg.DataSource = New DataTable()
            dgvNgOper.AutoGenerateColumns = False
            dgvNgOper.DataSource = New DataTable()
        End If
    End Sub

    Private Sub btnOper_Click(sender As Object, e As EventArgs) Handles btnOper.Click
        If dgv.SelectedRows.Count > 0 Then
            Dim formOper0 As New FormOper0()
            formOper0.usr = usr
            formOper0.source = "psqx1"
            formOper0.ty = "1"
            formOper0.b_gt_sys_id = dgv.SelectedRows(0).Cells("sys_id").Value.ToString()
            If DataGridView1.Rows.Count > 0 Then
                formOper0.pw_ok_num_all = DataGridView1.SelectedRows(0).Cells("Col_ok_num").Value.ToString()
            Else
                formOper0.pw_ok_num_all = "0"
            End If
            If DataGridView3.Rows.Count > 0 Then
                formOper0.oper_num_all = DataGridView3.SelectedRows(0).Cells("Col_oper_num").Value.ToString()
            Else
                formOper0.oper_num_all = "0"
            End If
            formOper0.ShowDialog(Me)
        End If
    End Sub

    Private Sub btnNgPw_Click(sender As Object, e As EventArgs) Handles btnNgPw.Click
        If dgv.SelectedRows.Count > 0 Then
            Dim formNgPw As New FormNgPw()
            formNgPw.usr = usr
            formNgPw.source = "psqx1"
            formNgPw.ty = "1"
            formNgPw.b_gt_sys_id = dgv.SelectedRows(0).Cells("sys_id").Value.ToString()
            formNgPw.picking_num = dgv.SelectedRows(0).Cells("picking_num").Value.ToString()
            If DataGridView1.Rows.Count > 0 Then
                formNgPw.ng_pw_num_all = DataGridView1.SelectedRows(0).Cells("C_ng_num").Value.ToString()
            Else
                formNgPw.ng_pw_num_all = "0"
            End If
            If DataGridView3.Rows.Count > 0 Then
                formNgPw.oper_num_all = DataGridView3.SelectedRows(0).Cells("Col_oper_num").Value.ToString()
            Else
                formNgPw.oper_num_all = "0"
            End If
            formNgPw.ShowDialog(Me)
        End If
    End Sub

    Private Sub btnValidityPw_Click(sender As Object, e As EventArgs) Handles btnValidityPw.Click
        If dgvNg.SelectedRows.Count > 0 Then
            If dgvNg.SelectedRows(0).Cells("Col_validity").Value.ToString().Equals("1") Then
                If MsgBox("确认要进行作废吗？", vbYesNo, "提示") = vbYes Then
                    Dim sys_id As String = dgvNg.SelectedRows(0).Cells("Col_sys_id").Value.ToString()
                    Dim ng_num As String = dgvNg.SelectedRows(0).Cells("Col_ng_num").Value.ToString()
                    Dim b_gt_sys_id As String = dgvNg.SelectedRows(0).Cells("Col_b_gt_sys_id").Value.ToString()
                    Dim procedure_no As String = dgvNg.SelectedRows(0).Cells("Col_procedure_no").Value.ToString()
                    Dim rtn As Integer = objGtNgReasonDetails.updateValidity(sys_id, ng_num, b_gt_sys_id, procedure_no, usr)
                    If rtn <> -2 Then
                        MsgBox("作废成功")
                        SetDgvResultData0()
                        setDgvNgData()
                    Else
                        MsgBox("作废失败")
                    End If
                End If
            Else
                MsgBox("该记录已作废")
            End If
        End If
    End Sub

    Private Sub btnValidityOper_Click(sender As Object, e As EventArgs) Handles btnValidityOper.Click
        If dgvOper.SelectedRows.Count > 0 Then
            If dgvOper.SelectedRows(0).Cells("Colvalidity").Value.ToString().Equals("1") Then
                If MsgBox("确认要进行作废吗？", vbYesNo, "提示") = vbYes Then
                    Dim sys_id As String = dgvOper.SelectedRows(0).Cells("Colsys_id").Value.ToString()
                    Dim procedure_no As String = dgvOper.SelectedRows(0).Cells("Colprocedure_no").Value.ToString()
                    Dim b_gt_sys_id As String = dgvOper.SelectedRows(0).Cells("Colb_gt_sys_id").Value.ToString()
                    Dim oper_num As String = dgvOper.SelectedRows(0).Cells("Coloper_num").Value.ToString()
                    Dim rtn As Integer = objGtProcedureDetails.updateValidity(sys_id, oper_num, b_gt_sys_id, procedure_no, usr)
                    If rtn <> -2 Then
                        MsgBox("作废成功")
                        setDgvOperData()
                        SetDgvResultData1()
                    Else
                        MsgBox("作废失败")
                    End If
                End If
            Else
                MsgBox("该记录已作废")
            End If
        End If
    End Sub

    Private Sub btnNg_Click(sender As Object, e As EventArgs) Handles btnNg.Click
        If dgvOper.SelectedRows.Count > 0 Then
            Dim formNgOper As New FormNgOper()
            formNgOper.usr = usr
            formNgOper.source = "psqx1"
            formNgOper.ty = "1"
            formNgOper.b_gt_sys_id = dgv.SelectedRows(0).Cells("sys_id").Value.ToString()
            formNgOper.current_procedure_No = 1
            If DataGridView2.Rows.Count > 0 Then
                formNgOper.ng_num_all = DataGridView2.SelectedRows(0).Cells("Co_ng_num").Value.ToString()
            Else
                formNgOper.ng_num_all = "0"
            End If
            If DataGridView3.Rows.Count > 0 Then
                formNgOper.oper_num_all = DataGridView3.SelectedRows(0).Cells("Col_oper_num").Value.ToString()
            Else
                formNgOper.oper_num_all = "0"
            End If
            formNgOper.ShowDialog(Me)
        End If
    End Sub

    Private Sub btnValidityNg_Click(sender As Object, e As EventArgs) Handles btnValidityNg.Click
        If dgvNgOper.SelectedRows.Count > 0 Then
            If dgvNgOper.SelectedRows(0).Cells("Colu_validity").Value.ToString().Equals("1") Then
                If MsgBox("确认要进行作废吗？", vbYesNo, "提示") = vbYes Then
                    Dim sys_id As String = dgvNgOper.SelectedRows(0).Cells("Colu_sys_id").Value.ToString()
                    Dim procedure_no As String = dgvNgOper.SelectedRows(0).Cells("Colu_procedure_no").Value.ToString()
                    Dim b_gt_sys_id As String = dgvNgOper.SelectedRows(0).Cells("Colu_b_gt_sys_id").Value.ToString()
                    Dim ng_num As String = dgvNgOper.SelectedRows(0).Cells("Colu_ng_num").Value.ToString()
                    Dim rtn As Integer = objGtNgReasonDetails.updateValidity(sys_id, ng_num, b_gt_sys_id, procedure_no, usr)
                    If rtn <> -2 Then
                        MsgBox("作废成功")
                        SetDgvResultData1()
                        setDgvNgOperData()
                    Else
                        MsgBox("作废失败")
                    End If
                End If
            Else
                MsgBox("该记录已作废")
            End If
        End If
    End Sub

    Private Sub btnFinish_Click(sender As Object, e As EventArgs) Handles btnFinish.Click
        If dgv.SelectedRows.Count > 0 Then
            If DataGridView3.Rows.Count > 0 Then
                If Decimal.Parse(dgv.SelectedRows(0).Cells("picking_num").Value.ToString()) - Decimal.Parse(DataGridView1.SelectedRows(0).Cells("C_ng_num").Value.ToString()) = Decimal.Parse(DataGridView3.SelectedRows(0).Cells("Col_oper_num").Value.ToString()) Then
                    If MsgBox("确认完工吗？", vbYesNo, "提示") = vbYes Then
                        Dim sys_id As String = dgv.SelectedRows(0).Cells("sys_id").Value.ToString()
                        Dim rtn As Integer = objGt.save("1", sys_id)
                        If rtn <> -2 Then
                            MsgBox("完工成功")
                            MsgBox("触发抛丸计划执行")
                            setData()
                        Else
                            MsgBox("完工失败")
                        End If
                    End If
                Else
                    MsgBox("领料数量需 = 抛丸不合格数 + 入料数量")
                End If
            Else
                MsgBox("请先进行入料！")
            End If
        End If
    End Sub

    Private Sub btnValidity_Click(sender As Object, e As EventArgs) Handles btnValidity.Click
        If dgv.SelectedRows.Count > 0 Then
            Dim rtn_validity As Integer = objGt.update_validity(dgv.SelectedRows(0).Cells("sys_id").Value.ToString())
            If rtn_validity <> -2 Then
                MsgBox("作废成功"， vbYes, "提示")
                setData()
            Else
                MsgBox("作废失败", vbYes, "提示")
            End If
        End If
    End Sub
End Class